﻿using System;
using IEx.Common.Sources;
using IEx.ParserBuilder.Facebook;
using IEx.ParserBuilder.GooglePlus;
using IEx.ParserBuilder.Twitter;
using IEx.ParserBuilder.Youtube;
using IEx;
using IEx.ParserBuilder.Otofun;
using IEx.ParserBuilder.Webtretho;
using IEx.ParserBuilder.Lamchame;
using IEx.ParserBuilder.Baomoi;
using IEx.ParserBuilder.Vatgia;
using IEx.ParserBuilder.Rongbay;
using IEx.ParserBuilder.Clipvn;
using IEx.ParserBuilder.FinanceReport;
using IEx.ParserBuilder.Tinhte;
using IEx.ParserBuilder.RestaurantFoody;
using IEx.ParserBuilder.Tamtay;
using IEx.ParserBuilder.Amthuc365;
using IEx.ParserBuilder.NhahangHanoi;
using IEx.ParserBuilder.MonngonMoingay;
using IEx.ParserBuilder.Chotot;
using IEx.ParserBuilder.Raovat;

namespace IEx
{
    public class SourceItem
    {
        protected IHtmlParser htmlParser;

        protected DefaultRequestBuilder requestBuilder;

        public SourceItem() { }

        public SourceItem(String url, int langId, string sourceCode, string searchCode, int sourceId, string datetimeFormat)
        {
            this.Url = url;
            this.LangId = langId;
            this.SourceCode = sourceCode;
            this.SourceId = sourceId;
            this.SearchCode = searchCode;
            this.DateTimeFormat = datetimeFormat;
        }

        public SourceItem(String url, int langId, string sourceCode, IHtmlParser htmlParser, DefaultRequestBuilder requestBuilder)
        {
            this.Url = url;
            this.LangId = langId;
            this.SourceCode = sourceCode;
            this.HtmlParserType = htmlParser.GetType().FullName;
            this.RequestBuilderType = requestBuilder.GetType().FullName;
            this.htmlParser = htmlParser;
            this.requestBuilder = requestBuilder;
        }

        public string RequestBuilderType
        {
            get;
            set;
        }

        public string Url
        {
            get;
            set;
        }

        public string DailyUrl
        {
            get;
            set;
        }

        public int LangId
        {
            get;
            set;
        }

        public string PrevResponseHtml
        {
            get;
            set;
        }

        public string SourceCode
        {
            get;
            set;
        }

        public string SearchCode { get; set; }

        public Nullable<int> Category { get; set; }

        public int SourceId { get; set; }

        public string DateTimeFormat { get; set; }

        public DateTime LastUpdated { get; set; }

        public long FailId { get; set; }

        public string HtmlParserType
        {
            get;
            set;
        }

        public virtual SourceItem Clone()
        {
            return new SourceItem(this.Url, this.LangId, this.SourceCode, this.htmlParser, this.requestBuilder);
        }

        public IHtmlParser HtmlParser
        {
            get
            {
                return this.GetHtmlParser();
            }
        }

        public DefaultRequestBuilder RequestBuilder
        {
            get
            {
                return this.GetRequestBuilder();
            }
        }

        public virtual IHtmlParser GetHtmlParser()
        {
            if (this.htmlParser != null)
                return this.htmlParser;

            switch (SourceId)
            {
                case (int)SourceIds.Facebook:
                    this.htmlParser = new FacebookParser();
                    break;
                case (int)SourceIds.Twitter:
                    this.htmlParser = new TwitterParser();
                    break;
                case (int)SourceIds.Youtube:
                    this.htmlParser = new YoutubeParser();
                    break;
                case (int)SourceIds.GooglePlus:
                    this.htmlParser = new GooglePlusParser();
                    break;
                case (int)SourceIds.Otofun:
                    this.htmlParser = new OtofunParser();
                    break;
                case (int)SourceIds.Webtretho:
                    this.htmlParser = new WebtrethoParser();
                    break;
                case (int)SourceIds.Lamchame:
                    this.htmlParser = new LamchameParser();
                    break;
                case (int)SourceIds.Baomoi:
                    this.htmlParser = new BaomoiParser();
                    break;
                case (int)SourceIds.Vatgia:
                    this.htmlParser = new VatgiaParser();
                    break;
                case (int)SourceIds.Rongbay:
                    this.htmlParser = new RongbayParser();
                    break;
                case (int)SourceIds.Clipvn:
                    this.htmlParser = new ClipvnParser();
                    break;
                case (int)SourceIds.HKExNews:
                    this.htmlParser = new HKExNewsReportParser();
                    break;
                case (int)SourceIds.Tinhte:
                    this.htmlParser = new TinhteParser();
                    break;
                case (int)SourceIds.Tamtay:
                    this.htmlParser = new TamtayParser();
                    break;
                case (int)SourceIds.Foody:
                    this.htmlParser = new FoodyParser();
                    break;
                case (int)SourceIds.Amthuc365:
                    this.htmlParser = new AmthucParser();
                    break;
                case (int)SourceIds.NhahangHanoi:
                    this.htmlParser = new NhahangHanoiParser();
                    break;
                case (int)SourceIds.MonngonMoingay:
                    this.htmlParser = new MonngonMoingayParser();
                    break;
                case (int)SourceIds.Chotot:
                    this.htmlParser = new ChototParser();
                    break;
                case (int)SourceIds.Raovat:
                    this.htmlParser = new RaovatParser();
                    break;
                default:
                    try
                    {
                        object obj = Activator.CreateInstance(Type.GetType(this.HtmlParserType));
                        if (obj is IHtmlParser)
                        {
                            this.htmlParser = (IHtmlParser)obj;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    break;
            }
            return this.htmlParser;
        }

        public virtual DefaultRequestBuilder GetRequestBuilder()
        {
            if (this.requestBuilder != null)
                return this.requestBuilder;

            switch (SourceId)
            {
                case (int)SourceIds.Facebook:
                    this.requestBuilder = new FacebookRequestBuilder();
                    break;
                case (int)SourceIds.Twitter:
                    this.requestBuilder = new TwitterRequestBuilder();
                    break;
                case (int)SourceIds.Youtube:
                    this.requestBuilder = new YoutubeRequestBuilder();
                    break;
                case (int)SourceIds.GooglePlus:
                    this.requestBuilder = new GooglePlusRequestBuilder();
                    break;
                case (int)SourceIds.Otofun:
                    this.requestBuilder = new OtofunRequestBuilder();
                    break;
                case (int)SourceIds.Webtretho:
                    this.requestBuilder = new WebtrethoRequestBuilder();
                    break;
                case (int)SourceIds.Lamchame:
                    this.requestBuilder = new LamchameRequestBuilder();
                    break;
                case (int)SourceIds.Baomoi:
                    this.requestBuilder = new BaomoiRequestBuilder();
                    break;
                case (int)SourceIds.Vatgia:
                    this.requestBuilder = new VatgiaRequestBuilder();
                    break;
                case (int)SourceIds.Rongbay:
                    this.requestBuilder = new RongbayRequestBuilder();
                    break;
                case (int)SourceIds.Clipvn:
                    this.requestBuilder = new ClipvnRequestBuilder();
                    break;
                case (int)SourceIds.HKExNews:
                    this.requestBuilder = new HKExNewsRequestBuilder();
                    break;
                case (int)SourceIds.Tinhte:
                    this.requestBuilder = new TinhteRequestBuilder();
                    break;
                case (int)SourceIds.Tamtay:
                    this.requestBuilder = new TamtayRequestBuilder();
                    break;
                case (int)SourceIds.Foody:
                    this.requestBuilder = new FoodyRequestBuilder();
                    break;
                case (int)SourceIds.Amthuc365:
                    this.requestBuilder = new AmthucRequestBuilder();
                    break;
                case (int)SourceIds.NhahangHanoi:
                    this.requestBuilder = new NhahangHanoiRequestBuilder();
                    break;
                case (int)SourceIds.MonngonMoingay:
                    this.requestBuilder = new MonngonMoingayRequestBuilder();
                    break;
                case (int)SourceIds.Chotot:
                    this.requestBuilder = new ChototRequestBuilder();
                    break;
                case (int)SourceIds.Raovat:
                    this.requestBuilder = new RaovatRequestBuilder();
                    break;
                default:
                    try
                    {
                        object obj = Activator.CreateInstance(Type.GetType(this.RequestBuilderType));
                        if (obj is DefaultRequestBuilder)
                        {
                            this.requestBuilder = (DefaultRequestBuilder)obj;
                            return this.requestBuilder;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    break;
            }
            return this.requestBuilder;
        }
    }

    public class SourceItemUpdate : SourceItem
    {
        public DateTime? DateFrom { get; set; }
        public DateTime? DateTo { get; set; }
        public int? FilterLanguageID { get; set; }

        public override DefaultRequestBuilder GetRequestBuilder()
        {
            if (this.requestBuilder != null)
                return this.requestBuilder;

            switch (SourceId)
            {
                case (int)SourceIds.Otofun:
                    // Keep original Request Builder for Custom Source
                    this.requestBuilder = new OtofunRequestBuilderUpdate(this.SearchCode, this.FilterLanguageID, this.DateFrom, this.DateTo);
                    break;
            }
            return this.requestBuilder;
        }

        public override IHtmlParser GetHtmlParser()
        {
            if (this.htmlParser != null)
                return this.htmlParser;

            switch (SourceId)
            {
                case (int)SourceIds.Otofun:
                    this.htmlParser = new OtofunParserUpdate(this.SearchCode, this.FilterLanguageID, this.DateFrom, this.DateTo);
                    break;
            }
            return this.htmlParser;
        }
    }
}
